home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16913 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  46 lines

  1. Path: newsfeed.internetmci.com!iol!usenet
  2. From: David Byrden <Goyra@iol.ie>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: STL: Finding an object in a set of Pointers
  5. Date: Fri, 12 Apr 1996 19:12:07 +0100
  6. Organization: Ireland On-Line
  7. Message-ID: <316E9CF7.1B52@iol.ie>
  8. References: <4klmjm$kul@access4.digex.net>
  9. NNTP-Posting-Host: dialup-348.dublin.iol.ie
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (Win95; I)
  14.  
  15. Mr. Blue wrote:
  16. > I am using an STL set to contain *pointers* to objects.
  17. > Problem: I want to find out if an object I just created is
  18. > logically contained in the set.
  19. > The find() function will tell me whether the same *address*
  20. > is already in the set, but I need to follow the pointers and
  21. > invoke operator==() on the objects.
  22. > Is there an elegant way I can do this without writing my
  23. > own loop, de-referencing, and comparing?
  24.  
  25. struct derefer_equal
  26. {
  27.       const Thing * myptr ;
  28.     derefer_equal( const Thing * p ) : myptr(p) {}
  29.     bool operator==( const Thing*& pr )
  30.     {
  31.         return (*myptr) == (*pr) ;
  32.     }
  33. } ;
  34.  
  35.  
  36.  
  37. Thing t1 ;
  38. vector<Thing*> vt ;
  39. find_if( vt.begin(), vt.end(), derefer_equal( &t1 ) ) ;
  40.  
  41.                     David
  42.